package com.smartsoft.demo.fragment;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.smartsoft.demo.R;
import com.smartsoft.demo.activity.VideoActivity;
import com.smartsoft.demo.domain.Contact;

/**
 * Contact preview fragment lets user to preview contact information.
 * Has transparent background.
 */
public class ContactPreviewFragment extends Fragment {

    //Contact key for saving it in bundle
    public static final String CONTACT = "contact";

    //controls for editing contact fields
    private ImageView avatar;
    private TextView firstName;
    private TextView lastName;
    private TextView phone;
    private TextView email;
    private TextView skype;
    private Button backButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle bundle) {
        View view = inflater.inflate(R.layout.contact_preview, container, false);
        //init controls for displaying fields
        avatar      = (ImageView)(view.findViewById(R.id.imageView));
        firstName   = (TextView) (view.findViewById(R.id.firstName));
        lastName    = (TextView) (view.findViewById(R.id.lastName));
        phone       = (TextView) (view.findViewById(R.id.phone));
        email       = (TextView) (view.findViewById(R.id.email));
        skype       = (TextView) (view.findViewById(R.id.skype));
        backButton  = (Button)   (view.findViewById(R.id.backButton));

        //go back to edit fragment
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });

        //get contact data from saved instance or fragment arguments
        if (bundle != null || ((bundle = getArguments()) != null)) {
            Contact contact = (Contact)bundle.get(CONTACT);
            if (contact != null) {
                fillFields(contact);
            }
        }
        return view;
    }

    /**
     * Fill controls with contact information
     * @param contact domain entity with information
     */
    private void fillFields(Contact contact) {
        if (contact.getAvatarUri() != null) {
            avatar.setImageURI(Uri.parse(contact.getAvatarUri()));
        }
        firstName.setText(contact.getFirstName());
        lastName.setText(contact.getLastName());
        email.setText(contact.getEmail());
        phone.setText(contact.getPhone());
        skype.setText(contact.getSkype());
    }

}